using System;
using System.Collections.Generic;
using System.Text;

namespace Coriander.Xmpp
{
    /// <summary>
    /// An XMPP message
    /// </summary>
    public class Message
    {
        readonly String value;

        Encoding encoding = Encoding.ASCII;

        /// <summary>
        /// Gets or sets the encoding to use
        /// </summary>
        public Encoding Encoding
        {
            get { return encoding; }
            set { encoding = value; }
        }

        /// <summary>
        /// The value
        /// </summary>
        public String Value
        {
            get { return this.value; }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public Message() : this(null) { }

        /// <summary>
        /// ctor : String
        /// </summary>
        /// <param name="value"></param>
        public Message(String value)
        {
            this.value = value;
        }

        /// <summary>
        /// Gets an array of bytes using the current Encoding object.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException" />
        public Byte[] GetBytes()
        {
            if (null == Encoding)
                throw new InvalidOperationException(
                    "The Encoding property is null. " +
                    "Set it before invoking this member."
                );

            return value != null ? Encoding.GetBytes(value) : new Byte[0];  
        }

        /// <summary>
        /// Converts a <see cref="Message">Message</see> to a <see cref="System.String">System.String</see>.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static implicit operator String(Message message)
        {
            return message.value;
        }
    }
}
